﻿using UnityEngine;

namespace CWM.Skinn
{
	public static partial class SkinnEx
    {
        private static class InputData
        {
            public static Vector2 lastMousePosition;
        }

        public static Vector3 GetSmoothDelta(this Event currentEvent)
        {
            var mousePosition = Event.current == null ? (Vector2)Input.mousePosition : Event.current.mousePosition;
            mousePosition += InputData.lastMousePosition / 2f;
            var delta = mousePosition - InputData.lastMousePosition;
            InputData.lastMousePosition = mousePosition;
            return delta;
        }

        public static Vector3 GetScreenInputPosition(this Event currentEvent)
        {
            Camera currentCamera = Camera.current;
            if (currentCamera == null || currentEvent == null) return Vector3.zero;
            Vector2 mousePos = new Vector2
            {
                x = currentEvent.mousePosition.x,
                y = currentCamera.pixelHeight - currentEvent.mousePosition.y
            };
            return currentCamera.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, currentCamera.nearClipPlane));
        }
    }
}